using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sprinting211 : SkillBasic
{
    public Transform playertransform;
    public PlayerBasic player;
    public PlayerAttack playerattack;
    private Rigidbody2D myRigidbody;
    public float damageratio;
    private TrailRenderer trail;
    public float skilltime;
    public int face;
    public float runspeedratio;
    private bool IsSkilling, IsSkilling2;
    public CameraFollow camerafollow;
    float gs;
    private BoxCollider2D collider;
    public int facememory;
    public float StiffTime;
    public float RepelDistance;
    public int RepelTimes;
    public float CD;
    private float CDtime;
    public bool skillend;
    public float resentdamageratio;
    bool skill2;
    // Start is called before the first frame update
    protected void Start()
    {
        playertransform = GetComponentInParent<Transform>();
        playerattack = GameObject.FindWithTag("Attack").GetComponent<PlayerAttack>();
        camerafollow = GameObject.Find("CameraFollow").GetComponent<CameraFollow>();
        myRigidbody = GetComponentInParent<Rigidbody2D>();
        trail = GetComponentInChildren<TrailRenderer>();
        player = GetComponentInParent<PlayerBasic>();
        collider = GetComponentInParent<BoxCollider2D>();
        trail.enabled = false;
        IsSkilling = false;
        collider.enabled = false;
        gs = myRigidbody.gravityScale;
        SprintingCD.CD = CD;
        SprintingCD.CDCurrent = 0;
        skillend = false;
    }
    public IEnumerator Skilling2()
    {
        yield return new WaitForSeconds(skilltime);
        player.attackable++;
        player.movable = true;
        trail.enabled = false;
        myRigidbody.velocity = new Vector2(0, myRigidbody.velocity.y);
        IsSkilling2 = false;
        collider.enabled = false;
        myRigidbody.gravityScale = gs;
        Time.timeScale = 1f;
        camerafollow.smoothing = 1f;
        playerattack.face = facememory;
        player.face = facememory;
        resentdamageratio = 0;
    }
    // Update is called once per frame
    public IEnumerator Skilling()
    {
        yield return new WaitForSeconds(skilltime);
        if (skill2)
        {
            skill2 = false;

        }
        else
        {
            IsSkilling = false;
            IsSkilling2 = true;
            StartCoroutine(Skilling2());

        }
    }
    public void Skill()
    {
        resentdamageratio = damageratio;
        camerafollow.smoothing = 0.1f;
        player.attackable --;
        player.movable = false;
        trail.enabled = true;
        IsSkilling = true;
        collider.enabled = true;
        facememory = player.face;
        StartCoroutine(Skilling());

    }
    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {

            other.GetComponent<EnemyBasic>().TakeDamage(resentdamageratio * playerattack.damage, face, RepelTimes, RepelDistance, StiffTime);
            if (resentdamageratio > 0) resentdamageratio -= 1f;
        }
        if (other.gameObject.CompareTag("GrindingTower"))
        {
            other.GetComponent<Grinding>().TakeDamage(damageratio * playerattack.damage);
            if (resentdamageratio > 0) resentdamageratio -= 1f;
        }
        if (other.gameObject.CompareTag("Air"))
        {
            myRigidbody.velocity = Vector2.zero;
            if (skill2 == false)
            {
                skill2 = true;
                IsSkilling = false;
                IsSkilling2 = true;
                StartCoroutine(Skilling2());
            }
        }
    }
    protected void Update()
    {
        face = player.face;
        if (CDtime > 0)
        {
            CDtime -= Time.deltaTime;
            SprintingCD.CDCurrent = CDtime;
        }
        if (Input.GetKeyDown(KeyCode) && Time.timeScale > 0)
        {
            if (CDtime <= 0)
            {
                Skill();
                CDtime = CD;
            }


        }
        if (IsSkilling)
        {
            Time.timeScale = 0.8f;
            myRigidbody.velocity = new Vector2(-face * runspeedratio * player.Runspeed, 0);
            myRigidbody.gravityScale = 0;
        }

        if (IsSkilling2)
        {
            playerattack.face = -facememory;
            player.face = -facememory;
            Time.timeScale = 0.8f;
            myRigidbody.velocity = new Vector2(facememory * runspeedratio * player.Runspeed, 0);
            myRigidbody.gravityScale = 0;
        }
    }
}
